package com.lk.presenter;

import android.os.Handler;

import com.lk.model.OnLoginListener;
import com.lk.model.User;
import com.lk.model.UserBiz;
import com.lk.view.ILoginView;

/**
 * Created by liu kun on 2018/7/24 0024.
 */

public class UserLoginPresenter {
    private UserBiz userBiz;
    private ILoginView loginView;
    private Handler mHandler=new Handler();

    public UserLoginPresenter(ILoginView loginView) {
        userBiz = new UserBiz();
        this.loginView = loginView;
    }
    public void login(){
        loginView.showLoading();
        userBiz.login(loginView.getName(), loginView.getPassword(), new OnLoginListener() {
            @Override
            public void loginSuccess(final User user) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.hideLoading();
                        loginView.toMainActivity(user);
                    }
                });
            }

            @Override
            public void loginFailed() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.hideLoading();
                        loginView.showFailedError();
                    }
                });
            }
        });
    }
    public void clear(){
        loginView.clearName();
        loginView.clearPassword();
    }
}
